#include "PhysicsManager.h"



PhysicsManager::PhysicsManager( const b2Vec2 & gravity ) {
	mWorld = new b2World(gravity);

	//From tutorial
	this->mPosIter = 2;
	this->mVelIter = 6;
}

void PhysicsManager::Update( float ts ) {
	if (!mWorld) {
		return;
	}

	mWorld->Step(ts,
		mVelIter,
		mPosIter);
}

//XXX Perhaps we can use the return value to return a hash value for
//easy indexing? Who knows right now.
int PhysicsManager::AddPhysicsObject (PhysicsPrimitive * inPrim,
										b2Body * inBody)
{
	//Need world for this...
	if (!inBody) {
		//Create body
		b2BodyDef bodyDef;
		bodyDef.type = inPrim->mProperties.mIsStatic ? b2_staticBody : b2_dynamicBody;
		bodyDef.position = inPrim->mProperties.mOrigin;

		inPrim->mBody = mWorld->CreateBody(&bodyDef);
	}
	
	//Create fixture
	b2FixtureDef fixtureDef;
	printf("Shape: %p\n", inPrim->mShape);
	fixtureDef.shape = inPrim->mShape;
	fixtureDef.density = inPrim->mProperties.mDensity;
	fixtureDef.friction = inPrim->mProperties.mFriction;

	inPrim->mFixture = inPrim->mBody->CreateFixture(&fixtureDef);	
	//Set fixture user data.
	inPrim->mFixture->SetUserData(&inPrim->mGameEntity);

	//XXX We need to facilitate deleting as well.
	//This should be different, just not sure how right now.
	mPrims.push_back(inPrim);

	return mPrims.size()-1;
}

float PhysicsManager::GetTimeStep() {
	return this->mTimeStep;
}
